﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Transactions;
using Traceability.App.Apilication.Common.Health;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure.Interfance;

namespace Traceability.App.Apilication.Handler.Health
{
    public class DelHealthHandler : IRequestHandler<DelHealthCommon, ApiResult<int>>
    {
        private readonly IBaseRespository<HealthModel> healthRepo;
        private readonly IMapper mapper;

        public DelHealthHandler(IBaseRespository<HealthModel> healthRepo,IMapper mapper)
        {
            this.healthRepo = healthRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelHealthCommon request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();

            var healthinfo = healthRepo.GetValue(request.Health_ID).Result;

            //var model= mapper.Map(request,healthinfo);
            healthinfo.Delete_Identifier=true;
            var r = 0;
            using (TransactionScope tran=new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                r= healthRepo.Update(healthinfo).Result;
                tran.Complete();
            };

            if (r > 0)
            {
                res.Code = ApiEnum.Success;
                res.Message = "删除成功";
                return Task.FromResult(res);
            }
            else
            {
                res.Code = ApiEnum.Error;
                res.Message = "删除失败";
                return Task.FromResult(res);
            }

        }
    }
}
